﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration.Install;
using System.ServiceProcess;
using Microsoft.Win32;
using System.Management;
using System.Runtime.InteropServices;


namespace AleScheduler_service
{
    [RunInstaller(true)]
    public partial class ProjectInstaller : Installer
    {
        public ProjectInstaller()
        {
            //Create the installer that will configure the windows service
            //by writing some values in the registry
            //This class is specific to a single Windows Service
            ServiceInstaller myServiceInstaller = new ServiceInstaller();
            //This should be the same name as what you specified in the service constructor
            myServiceInstaller.ServiceName = "AleScheduler_Service";
            myServiceInstaller.DisplayName = "AleScheduler";
            myServiceInstaller.Description = "AleScheduler is a Windows Service that allow you to schedule Command, Application or Script.";
            myServiceInstaller.StartType = ServiceStartMode.Automatic;
            this.Installers.Add(myServiceInstaller);

            //Create the installer that will register the windows service
            //This class could register many services
            ServiceProcessInstaller myProcessInstaller = new ServiceProcessInstaller();
            myProcessInstaller.Account = ServiceAccount.LocalSystem;
            myProcessInstaller.AfterInstall += new InstallEventHandler(AleSchedulerProcessInstaller1_AfterInstall);
            this.Installers.Add(myProcessInstaller);
        
        }

        private void AleSchedulerProcessInstaller1_AfterInstall(object sender, InstallEventArgs e)
        {
            string serviceName = "AleScheduler_Service";

            RegistryKey key = Registry.LocalMachine.OpenSubKey(@"SYSTEM\CurrentControlSet\Services\" + serviceName + @"\", true);

            if (key != null)
            {
                // get the value of the Type key.
                object obj = key.GetValue("Type");

                if (obj != null && obj is int)
                {
                    int value = (int)obj;

                    // set the flag.
                    value |= 0x100;

                    // set the value back.
                    key.SetValue("Type", value);
                }
            }
        }

    }
}
